﻿#pragma once

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include "shader/BaseShader.h"
#include <map>

extern "C"
{
#include <libavformat/avformat.h>
}

class OpenGLEffectsView : public QOpenGLWidget, protected QOpenGLFunctions
{
	Q_OBJECT

public:
	OpenGLEffectsView(QWidget* parent = nullptr);
	~OpenGLEffectsView();


	virtual void Init(int width, int height);

	//不管成功与否都释放frame空间
	virtual void Repaint(AVFrame* frame);

	//绑定特效
	void BindEffects();

	//释放特效
	void ReleaseEffects();


	bool isDrawEnd() {
		return is_draw_end;
	}

	void SetEffectsType(OpenGLEffects effectsType);

	void SetThresholdValue(float value)
	{
		this->threshold_value = value;
	}

	void SetGammaValue(float value)
	{
		this->gamma_value = value;
	}

	void setNostalgiaValue(float intensity, float grainAmount)
	{
		this->intensity = intensity;
		this->grainAmount = grainAmount;
	}
	void SetBrightnessExposureContrastSaturation(float brightness, float exposure, float contrast, float saturation)
	{
		this->brightness = brightness;
		this->exposure = exposure;
		this->contrast = contrast;
		this->saturation = saturation;
	}

	void SetCtrlL(float shadow, float midtone, float highlight)
	{
		this->shadow = shadow;
		this->midtone = midtone;
		this->highlight = highlight;
	}

protected:
	//刷新显示
	void paintGL();

	//初始化gl
	void initializeGL();

	// 窗口尺寸变化
	void resizeGL(int width, int height);
private:
	//std::mutex mux;
	//openg的 texture地址
	BaseShader* baseShader = nullptr;

	GLuint texs[3] = { 0 };

	//材质内存空间
	unsigned char* datas[3] = { 0 };

	int width = 240;
	int height = 128;

	//是否绘制完成
	bool is_draw_end = true;

	//切换特效标志(默认原图)
	OpenGLEffects effectsType = OpenGLEffects::ORIGINAL;

	//std::map<OpenGLEffects, BaseShader*> effects;
	//阈值
	float threshold_value = 0.5;
	//gamma值
	float gamma_value = 1.0f;
	//怀旧
	float intensity = 0.8f;//效果强度,增强怀旧感(0.0~1.0)越大越好
	float grainAmount = 0.15f;//颗粒感强度,减少噪感（0.0~1.0）越小越好

	//亮度、曝光度、对比度、饱和度
	float brightness = 0.0f;// 亮度 (-1.0~1.0)，默认0.0f表示不调节亮度
	float exposure = 0.0;//曝光 (-1.0~1.0),默认0.0f表示不调节曝光度
	float contrast = 1.0f;//对比度 (0.0~2.0, 默认1.0)默认1.0表示不调节
	float saturation = 1.0f;//饱和度 (0.0~2.0, 默认1.0),默认1.0表示不调节饱和度

	//色阶调节，取值范围（0~1）
	float shadow = 0.0f;    // 黑场阈值（低于此值的像素变为纯黑）
	float midtone = 1.0f;   // 灰场gamma值（控制中间调亮度）,亮度调节的值可以><1
	float highlight = 1.0f; // 白场阈值（高于此值的像素变为纯白）

};
